package controller

import (
	"encoding/json"
	"reflect"
	"strconv"
)

func (this BaseController) GetParam() map[string]interface{} {
	var formData map[string]interface{}
	var bodyData map[string]interface{}
	err := this.ParseForm(&formData)
	if err != nil {
		println("参数转化错误：", err.Error())
	}
	bodyData = this.GetBodyParamAll()
	for key, formItem := range formData {
		bodyData[key] = formItem
	}
	return bodyData
}
func (this BaseController) GetParamJsonString() string {
	var formData map[string]interface{}
	var bodyData map[string]interface{}
	err := this.ParseForm(&formData)
	if err != nil {
		println("参数转化错误：", err.Error())
	}
	bodyData = this.GetBodyParamAll()
	for key, formItem := range formData {
		bodyData[key] = formItem
	}
	str, err := json.Marshal(bodyData)
	if err != nil {
		return ""
	}
	return string(str)
}

func (this *BaseController) GetParamBool(name string) bool {
	find := this.Ctx.Input.Query(name)
	if find != "" {
		v,_ := strconv.ParseBool(find)
		return v
	}
	findFromBody := this.GetBodyParamBool(name)
	return findFromBody
}

func (this BaseController) GetParamString(name string) string {
	find := this.GetString(name)
	if find != "" {
		return find
	}
	find = this.GetBodyParamString(name)
	return find
}
func (this BaseController) GetParamStrings(name string) []string {
	find := this.GetStrings(name)
	if len(find) > 0 {
		return find
	}
	all := this.GetBodyParamSliceString(name)
	//find, ok := all[name].([]string)
	//if !ok {
	//	return make([]string, 0)
	//}
	return all
}

func (this BaseController) GetParamInt(name string) int {
	find, _ := this.GetInt(name, 0)
	if find != 0 {
		return find
	}
	all := this.GetBodyParamAll()
	find64, ok := all[name].(float64)
	if !ok {
		return 0
	}
	return int(find64)
}
func (this BaseController) GetParamIntSlice(name string) []int {
	var find = make([]int, 0)
	c := this
	f, _ := c.Input()
	if f != nil {
		tmp, ok := f[name]
		if ok {
			for _, s := range tmp {
				s2i, err := strconv.Atoi(s)
				if err != nil {
					continue
				}
				find = append(find, s2i)
			}
			return find
		}

	}
	all := this.GetBodyParamIntSlice(name)
	return all
}

func (this BaseController) GetParamInt64(name string) int64 {
	find, _ := this.GetInt64(name, 0)
	if find != 0 {
		return find
	}
	all := this.GetBodyParamAll()
	find64, ok := all[name].(float64)
	if !ok {
		return 0
	}
	return int64(find64)
}
func (this BaseController) GetParamInt64Slice(name string) []int64 {
	var find []int64
	//var form map[string]interface{}
	//err := this.ParseForm(&form)
	//if err == nil {
	//	find, ok := form[name].([]int64)
	//	if !ok {
	//		return make([]int64, 0)
	//	} else {
	//		return find
	//	}
	//} else {
	//	println("GetParamIntSlice ParseForm ERROR" + err.Error())
	//}
	all := this.GetBodyParamAll()
	tmp, ok := all[name].([]interface{})
	//tool.JsonLog(reflect.TypeOf(all[name]).String())
	if !ok {
		return make([]int64, 0)
	} else {
		for _, t := range tmp {
			v, _ := t.(float64)
			find = append(find, int64(v))
		}
	}
	return find
}

func (this BaseController) GetParamInt32(name string) int32 {
	find, err := this.GetInt32(name, 0)
	if err == nil {
		return find
	}
	all := this.GetBodyParamAll()
	find, ok := all[name].(int32)
	if !ok {
		return 0
	}
	return find
}
func (this BaseController) GetParamInt32Slice(name string) []int32 {
	var find []int32
	//var form map[string]interface{}
	//err := this.ParseForm(&form)
	//if err == nil {
	//	find, ok := form[name].([]int32)
	//	if !ok {
	//		return make([]int32, 0)
	//	} else {
	//		return find
	//	}
	//} else {
	//	println("GetParamIntSlice ParseForm ERROR" + err.Error())
	//}
	all := this.GetBodyParamAll()
	find, ok := all[name].([]int32)
	if !ok {
		return make([]int32, 0)
	}
	return find
}
func (this BaseController) GetParamFloat64(name string) float64 {
	find, err := this.GetFloat(name)
	if err == nil {
		return find
	}
	all := this.GetBodyParamAll()
	find, ok := all[name].(float64)
	if !ok {
		return 0
	}
	return find
}
func (this BaseController) GetParamFloat64Slice(name string) []float64 {
	var find []float64
	//var form map[string]interface{}
	//err := this.ParseForm(&form)
	//if err == nil {
	//	find, ok := form[name].([]float64)
	//	if !ok {
	//		return make([]float64, 0)
	//	} else {
	//		return find
	//	}
	//} else {
	//	println("GetParamIntSlice ParseForm ERROR" + err.Error())
	//}
	all := this.GetBodyParamAll()
	find, ok := all[name].([]float64)
	if !ok {
		return make([]float64, 0)
	}
	return find
}

func (this BaseController) GetParamMapString(name string) map[string]interface{} {
	var find map[string]interface{}
	var form map[string]interface{}
	err := this.ParseForm(&form)
	if err == nil {
		find, ok := form[name].(map[string]interface{})
		if !ok {
			return make(map[string]interface{})
		} else {
			return find
		}
	} else {
		println("GetParamIntSlice ParseForm ERROR" + err.Error())
	}
	all := this.GetBodyParamAll()
	find, ok := all[name].(map[string]interface{})
	if !ok {
		return make(map[string]interface{})
	}
	return find
}

func (this BaseController) GetParamMapSlice(name string) []map[string]interface{} {
	var data map[string]interface{}
	this.Try(json.Unmarshal(this.Ctx.Input.RequestBody, &data))
	tmp,ok:=data[name]
	if ok {
		t:=reflect.TypeOf(tmp)
		if t.String() == "[]interface {}" {
			ids := make([]map[string]interface{},0)
			for _, test := range tmp.([]interface{}) {
				m,ok:=test.(map[string]interface{})
				if ok {
					ids = append(ids,m)
				}
			}
			return ids
		}
	}
	return make([]map[string]interface{},0)
}